﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T12_LockApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TestLock testLock = new TestLock();
            testLock.StartCounter();

            Console.ReadLine();
        }
    }

    class TestLock
    {
        private int num = 0;
        private object myLock = new object();
        public void StartCounter()
        {
            for (int i = 0; i < 5; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    TestCount();
                });
            }
        }

        public void TestCount()
        {
            for (int i = 0; i < 100; i++)
            {
                //lock (myLock)
                //{
                //    num++;
                //    Console.WriteLine(num);
                //}

                lock (this)
                {
                    num++;
                    Console.WriteLine(num);
                }
            }
        }
    }
}
